/**
 * 
 */
package com.mc.fastjson;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author w.weizhen
 *
 */
public class WriteFile {
	/**
     * @param args
     */
    public static void main(String[] args) {
        //txt转json
//        String evidence = "{\"summary\":[\"1338/1601256950323.jpeg\"],\"diagnosis\":[\"1338/1601256950343.jpeg\",\"1549/1602555028709.jpg\",\"1549/1602555028688.jpeg\",\"1009/1599790520258.jpg\"]}\n";
//        JSONObject obj = JSON.parseObject(evidence);
//        String diagnosis =obj.getString("diagnosis");
//        System.out.println(diagnosis);
        
        //json转txt,转List<String>
        JSONArray evidence = new JSONArray();
        evidence.add("487/7eac7f7a30f4f0a911880a2edcb22059.jpg");
        evidence.add("487/a24b040723469c0681d75250ba155d7e.jpg");
        evidence.add("487/afefac3b9dbdf36a0a4f42a52ac2135a.jpg");
        String str=evidence.toJSONString();
        System.out.println(str);
        List<String> list_str=JSONArray.parseArray(str,String.class);
        System.out.println(list_str.toString());
        
        /* json字符串转文件和文件转json字符串 */
        JSONArray array = new JSONArray();
        JSONObject object=null;
        for (int i = 0; i < 100; i++)
        {
            object=new JSONObject();
            object.put("name", "name"+i);
            object.put("age", "age"+i);
            object.put("sex", "sex"+i);
            object.put("build", "build"+i);
            array.add(object);
        }
        System.out.println(array.toString());
        
        try
        {
            //json集合生成本地json文件
            FileUtils.writeStringToFile(new File("D:\\2020\\2020-01\\2020-01-01.json"), array.toString(), "UTF-8");
            //读取本地json文件，转为json集合
            String fastjson=FileUtils.readFileToString(new File("D:\\2020\\2020-01\\2020-01-01.json"), "UTF-8");
            array = JSON.parseArray(fastjson);
            System.out.println(array.toString());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
